<?php
/**
 * 字符串，文档处理等工具集合类
 * User: Administrator
 * Date: 2015-3-11
 * Time: 11:23
 */

namespace common\components\helpers;


use yii\helpers\ArrayHelper;
use yii\helpers\Url;

class Util {

    /**
     * 将菜单数组转成系统url地址
     * @param array $menu
     * @return string
     */
    public static function createMenuUrl($menu,$data=[],$param=[]){
        $url=trim($menu['m'].'/'.$menu['c'].'/'.$menu['a'],'/');
        $url='/'.$url;
        if(!empty($menu['param'])){
            $menuParam=$menu['param'];
            foreach($data as $k=>$v){
                $menuParam=str_replace('{$'.$k.'}',$v,$menuParam);
            }
            $url=ArrayHelper::merge([$url],Util::param2array($menuParam));
        }else{
            $url=[$url];
        }
        return Fun::url($url);
    }


    /**
     * 将get地址参数转化为数组
     *
     * @param array $data
     * @param bool $isformdata
     * @return string
     */
    public static function param2array($data) {
        if (empty($data))return null;
        if(strstr($data,'&')){
            $a=explode('&',$data);
            $param_array=array();
            foreach ($a as $v){
                $b=explode('=',$v);
                $param_array[$b[0]]=$b[1];
            }
            return $param_array;
        }
        else{
            if(substr_count($data, '=')==1){
                $b=explode('=',$data);
                $param_array[$b[0]]=$b[1];
                return $param_array;
            }
            else{
                return null;
            }
        }
    }

    /**
     * 将数组转化为get地址参数
     *
     * @param array $data
     * @param bool $isformdata
     * @return string
     */
    public static function array2param($data) {
        if (empty($data))return null;
        if (!is_array($data))return null;
        $param_array=[];
        foreach($data as $k=>$v){
            $param_array[$k]=$k.'='.$v;
        }
        return count($param_array)>0?implode('&',$param_array):'';
    }


    public static function text2array($data){
        if(empty($data))return null;
        $str=preg_replace("/\s+/", ",", $data);
        $text_array=explode(',', $str);
        $text_array=array_filter($text_array);
        $result_array=array();
        foreach ($text_array as $v){
            if(substr_count($v, '|')==1){
                $tmp_array=explode('|', $v);
                $result_array[$tmp_array[0]]=$tmp_array[1];
            }
        }
        return $result_array;
    }

    public static function array2text($data){
        if(!is_array($data))return null;
        $text='';
        foreach ($data as $k=>$v){
            if(empty($text)){
                $text.=$k.'|'.$v;
            }
            else{
                $text.="\r\n".$k.'|'.$v;
            }
        }
        return $text;
    }
	
	/*
     * 数据补零方法
     * $str 需要补充的数据
     * $length 补零后数据的长度
     * */
    public static function zeroFill($str,$length)
    {
        return str_pad($str,$length,'0',STR_PAD_LEFT);
    }

    //班級上課形式轉換
    public static function getModality($modality)
    {
        $currentModality = '';
        switch ($modality) {
            case 0:
                $currentModality = '一对一';
                break;
            case 1:
                $currentModality = '小班制';
                break;
            case 2:
                $currentModality = '普通班';
                break;
        }
        return $currentModality;
    }

    //星期转换
    public static function getWeek($week)
    {
        $currentWeek = '';
        switch ($week) {
            case 1:
                $currentWeek = '周一';
                break;
            case 2:
                $currentWeek = '周二';
                break;
            case 3:
                $currentWeek = '周三';
                break;
            case 4:
                $currentWeek = '周四';
                break;
            case 5:
                $currentWeek = '周五';
                break;
            case 6:
                $currentWeek = '周六';
                break;
            case 7:
                $currentWeek = '周日';
                break;
        }
        return $currentWeek;
    }

    //星期转换
    public static function getWeekday($weekday)
    {
        $currentWeekDay = 0;
        switch ($weekday) {
            case '星期一':
                $currentWeekDay = 1;
                break;
            case '星期二':
                $currentWeekDay = 2;
                break;
            case '星期三':
                $currentWeekDay = 3;
                break;
            case '星期四':
                $currentWeekDay = 4;
                break;
            case '星期五':
                $currentWeekDay = 5;
                break;
            case '星期六':
                $currentWeekDay = 6;
                break;
            case '星期天':
                $currentWeekDay = 7;
                break;
        }
        return $currentWeekDay;
    }

    //时间格式转换
    public static function getTime($time = '')
    {
        if (empty($time)) {
            $currentTime = '';
        } else {
            $currentTime = strlen($time) == 3 ? substr($time, 0, 1) . ':' . substr($time, 1) : substr($time, 0, 2) . ':' . substr($time, 2);
        }
        return $currentTime;
    }

    //浮点数是否相等
    public static function floatcmp($f1,$f2,$precision = 10) // are 2 floats equal
    {
        $e = pow(10,$precision);
        $i1 = intval($f1 * $e);
        $i2 = intval($f2 * $e);
        return ($i1 == $i2);
    }
    public static function floatgtr($big,$small,$precision = 10) // is one float bigger than another
    {
        $e = pow(10,$precision);
        $ibig = intval($big * $e);
        $ismall = intval($small * $e);
        return ($ibig > $ismall);
    }
    public static function floatgtre($big,$small,$precision = 10) // is on float bigger or equal to another
    {
        $e = pow(10,$precision);
        $ibig = intval($big * $e);
        $ismall = intval($small * $e);
        return ($ibig >= $ismall);
    }
    public static function getLastAfternoon($time){
        if(strpos($time,':')){
            $parts = explode(':', (string)$time);
            $h = (int)array_shift($parts);//array_shift是弹出第一个，array_pop是弹出最后一个
            if ($h<12) return '上午';
            else return '下午';
        }else{
            return '';
        }

    }

}